﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using ECCIT;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.BLL;
using OgilvyOne.UKNow.Configuration;
using ECCIT.Utilities;

public partial class GateService_GalleryInterface : System.Web.UI.Page
{
    private const string ELEMENT_FILE =
        "<file type=\"{0}\" url=\"{1}\" thumb=\"{2}\" avatar=\"{3}\" errorPic=\"{4}\" errorThumbPic=\"{5}\" title=\"{6}\" city=\"{7}\"/>";

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/xml";
        Response.ContentEncoding = Encoding.UTF8;

        string output = string.Empty;
        try
        {
            ResultInfo valRes = ValidateArguments();

            if (valRes.Result)
            {
                string sLangID = Request.Params["lang"];
                int langID = (string.IsNullOrEmpty(sLangID)) ? 2052 : int.Parse(sLangID);
                EventsImageBLL imageBll = new EventsImageBLL(langID);
                EventsVideoBLL videoBll = new EventsVideoBLL(langID);

                StringBuilder builder = new StringBuilder("<files>");

                IList<EventsImageModel> images = imageBll.GetPublishedGalleryImageModels();
                foreach (EventsImageModel image in images)
                {
                    builder.Append(RenderImage(image.Path, image.Path1, image.Avatar, image.Events.Title,
                                               (langID == 2052)
                                                   ? image.Events.CityModel.Cnname
                                                   : image.Events.CityModel.Enname));
                }

                IList<EventsVideoModel> videos = videoBll.GetPublishedGalleryVideoModels();
                foreach (EventsVideoModel video in videos)
                {
                    if (video.Type == 1)
                        builder.Append(RenderVideo(video.Path, video.ThumbnailImagePath, video.Events.Title,
                                                   (langID == 2052)
                                                       ? video.Events.CityModel.Cnname
                                                       : video.Events.CityModel.Enname));
                    else if (video.Type == 3)
                        builder.Append(RenderAudio(video.Path, video.ThumbnailImagePath, video.Events.Title,
                                                   (langID == 2052)
                                                       ? video.Events.CityModel.Cnname
                                                       : video.Events.CityModel.Enname));
                }

                builder.Append("</files>");

                output = builder.ToString();
            }
        }
        catch (Exception ex)
        {
            output = "<files value=\"false\"></files>";
        }

        Response.Write("<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + output);
    }

    private ResultInfo ValidateArguments()
    {
        ResultInfo result = new ResultInfo(false);
        string sLangID = Request.Params["lang"];
        if (string.IsNullOrEmpty(sLangID)) return result;

        int langID;
        bool b = int.TryParse(sLangID, out langID);
        if (!b) return result;
        if (langID != 2052 && langID != 1033) return result;

        result.Result = true;
        return result;
    } 


    private string RenderImage(string url, string thumbUrl, string avatarUrl, string title, string city)
    {
        return string.Format(ELEMENT_FILE, 1,
                             ConfigCacheManager.FilePath.UploadImagePath.VirtualPath + "/" + url,
                             ConfigCacheManager.FilePath.UploadImagePath.VirtualPath + "/" + thumbUrl,
                             string.IsNullOrEmpty(avatarUrl)
                                 ? ""
                                 : ConfigCacheManager.FilePath.UploadImagePath.VirtualPath + "/" + avatarUrl,
                             "",
                             ConfigCacheManager.FileName.DefaultEventsImageThumbImage, HttpUtility.HtmlEncode(title),
                             city);
    }

    private string RenderVideo(string url, string thumbUrl, string title, string city)
    {
        return string.Format(ELEMENT_FILE, 2,
                             ConfigCacheManager.FilePath.UploadMediaPath.VirtualPath + "/" + url,
                             ConfigCacheManager.FilePath.UploadImagePath.VirtualPath + "/" + thumbUrl,
                             "",
                             "",
                             ConfigCacheManager.FileName.DefaultEventsVideoThumbImage, HttpUtility.HtmlEncode(title),
                             city);
    }

    private string RenderAudio(string url, string thumbUrl, string title, string city)
    {
        return string.Format(ELEMENT_FILE, 3,
                             ConfigCacheManager.FilePath.UploadMediaPath.VirtualPath + "/" + url,
                             ConfigCacheManager.FilePath.UploadImagePath.VirtualPath + "/" + thumbUrl,
                             "",
                             "",
                             ConfigCacheManager.FileName.DefaultEventsAudioThumbImage, HttpUtility.HtmlEncode(title.Replace("&lt;","").Replace("&gt;","")),
                             city);
    }
}
